
(function(){
    var CommonMapUtil = window.CommonMapUtil = function(div,onMapLoaded){
        return new CommonMapUtil.prototype.init(div,onMapLoaded);
    };
    CommonMapUtil.fn = CommonMapUtil.prototype = {
    	layer1:null,
    	layer2:null,
    	mapDiv:null,
    	map:null,
    	view:null,
    	mapLoaded:null,
        init:function (div,onMapLoaded){
        	if(typeof onMapLoaded=="function"){
        		this.mapLoaded=onMapLoaded;
        	}
        	this.initUI();
        	this.initUIEvent();
        	this.mapDiv=div;
        	this.showMap1();
            return this;
        },

        initUI:function(){

            //修改坐标居中
            var width=$(document).width();
            var corrdWidth=width-380;
            $(".showCorrd").css("left",corrdWidth/2+"px");
        },

        initUIEvent:function(){
    	    var that=this;
            //影像矢量地图切换
            $('#js-switch-map').on('click', function(){
                var $this = $(this);
                if($this.hasClass('map-tool-switch-map-yx')){
                    $this.removeClass('map-tool-switch-map-yx');
                    $this.find('p').text('矢量');
                    that.map.basemap=that.imgBasemap;
                }else{
                    $this.addClass('map-tool-switch-map-yx');
                    $this.find('p').text('影像');
                    that.map.basemap=that.vecBasemap;
                }
            });

        },


		/**
		 * 加载天地图数据
		 */
        showMap1:function () {
            var that=this;
            require([
                    "esri/config",
                    "esri/core/urlUtils",
                    "esri/Map",
                    "esri/views/MapView",
                    "esri/layers/MapImageLayer",
                    "esri/layers/TileLayer",
                    "esri/layers/support/TileInfo",
                    "esri/layers/WebTileLayer",
                    "esri/geometry/SpatialReference",
                    "esri/geometry/Extent",
                    "esri/geometry/Point",
                    "dojo/dom",
                    "dojo/parser",
                    "dijit/registry",
                    "esri/Basemap",
                    "esri/widgets/BasemapToggle",
                    "esri/layers/VectorTileLayer",
                    "dojo/domReady!"],
                function (
                    esriConfig,
                    urlUtils,
                    Map,
                    MapView,
                    MapImageLayer,
                    TileLayer,
                    TileInfo,
                    WebTileLayer,
                    SpatialReference,
                    Extent,
                    Point,
                    dom,
                    parser,
                    registry,
                    Basemap,
                    BasemapToggle,
                    VectorTileLayer
                ) {
                var ips=CommonUtils.getProxyIP();
                for(var i=0;i<ips.length;i++){
                    esriConfig.request.corsEnabledServers.push(ips[i]);
                }
                    urlUtils.addProxyRule({
                        urlPrefix: "http://112.54.43.227:92",
                        proxyUrl:  CommonUtils.getProxyUrl()
                    });
                    that.map = new Map();
                var scale="";
                var center="";
               var centerPt = null;
                if(mapConfig.coordinates=="projected"){
                    scale=mapConfig.projectConfig.scale;
                    center=mapConfig.projectConfig.center;
                    centerPt = new Point({
                        x:mapConfig.projectConfig.center[0],
                        y: mapConfig.projectConfig.center[1],
                        spatialReference: mapConfig.projectConfig.spatialReference
                    });
                }else  if(mapConfig.coordinates=="geodesic") {
                    scale=mapConfig.geodesicConfig.scale;
                    center=mapConfig.geodesicConfig.center;
                    centerPt = new Point({
                        x:mapConfig.projectConfig.center[0],
                        y: mapConfig.projectConfig.center[1],
                        spatialReference: mapConfig.projectConfig.spatialReference
                    });
                }
                    that.view = new MapView({
                        "map": that.map,
                        "container": "mapdiv",
                        "center": center,
                        "ui":{
                            "components":["home","zoom"]
                        }
                    });

                    that.view.when(function(instance){
                        that.mapLoaded(instance);
                        that.registerMapEvents();
                        that.view.scale= scale;
                        that.view.goTo(centerPt);
                    }, function(error){
                        console.log('地图加载失败: ', error);
                    });

                    var vecLayer=null;
                    var vecLayer_anno=null;
                    var imgLayer=null;
                    var imgLayer_anno=null;
                if(mapConfig.coordinates=="projected"){
                    vecLayer=new TileLayer({
                        id:mapConfig.projectConfig.baseMaps.vecRestService.identifier,
                        title:mapConfig.projectConfig.baseMaps.vecRestService.title,
                        name:mapConfig.projectConfig.baseMaps.vecRestService.name,
                        url:mapConfig.projectConfig.baseMaps.vecRestService.url,
                        visible:true
                    });

                    imgLayer=new TileLayer({
                        id:mapConfig.projectConfig.baseMaps.imgRestService.identifier,
                        title:mapConfig.projectConfig.baseMaps.imgRestService.title,
                        name:mapConfig.projectConfig.baseMaps.imgRestService.name,
                        url:mapConfig.projectConfig.baseMaps.imgRestService.url,
                        visible:true
                    });
                }else  if(mapConfig.coordinates=="geodesic"){
                    var tileInfo = new TileInfo(mapConfig.geodesicConfig.tileInfo);
                     vecLayer=new WebTileLayer({
                     "id":mapConfig.geodesicConfig.baseMaps.vector.identifier,
                     "urlTemplate":mapConfig.geodesicConfig.baseMaps.vector.urlTemplate,
                     "subDomains":mapConfig.geodesicConfig.baseMaps.vector.subDomains,
					 "tileInfo":tileInfo,
					 "spatialReference":mapConfig.geodesicConfig.wkid
                   });
                    vecLayer_anno=new WebTileLayer({
                        "id":mapConfig.geodesicConfig.baseMaps.vector_anno.identifier,
                        "urlTemplate":mapConfig.geodesicConfig.baseMaps.vector_anno.urlTemplate,
                        "subDomains":mapConfig.geodesicConfig.baseMaps.vector_anno.subDomains,
                        "tileInfo":tileInfo,
                        "spatialReference":mapConfig.geodesicConfig.wkid
                    });
                    imgLayer=new WebTileLayer({
                        "id":mapConfig.geodesicConfig.baseMaps.image.identifier,
                        "urlTemplate":mapConfig.geodesicConfig.baseMaps.image.urlTemplate,
                        "subDomains":mapConfig.geodesicConfig.baseMaps.image.subDomains,
                        "tileInfo":tileInfo,
                        "spatialReference":mapConfig.geodesicConfig.wkid
                    });
                    imgLayer_anno=new WebTileLayer({
                        "id":mapConfig.geodesicConfig.baseMaps.image_anno.identifier,
                        "urlTemplate":mapConfig.geodesicConfig.baseMaps.image_anno.urlTemplate,
                        "subDomains":mapConfig.geodesicConfig.baseMaps.image_anno.subDomains,
                        "tileInfo":tileInfo,
                        "spatialReference":mapConfig.geodesicConfig.wkid
                    });
                    //  =new WebTileLayer({
                    //     "id":mapConfig.geodesicConfig.baseMaps.image.identifier,
                    //      "urlTemplate":mapConfig.geodesicConfig.baseMaps.image.urlTemplate,
                    //     "tileInfo":tileInfo,
                    //     "spatialReference":mapConfig.geodesicConfig.wkid
                    // });
                }

                    that.vecBasemap = new Basemap({
                        baseLayers: [vecLayer,vecLayer_anno],
                        thumbnailUrl:'img/vec_thum.jpg',
                        id: "vecBasemap"
                    });
                    that.imgBasemap = new Basemap({
                        baseLayers: [imgLayer,imgLayer_anno],
                        thumbnailUrl:'img/img_thum.jpg',
                        id: "imgBasemap"
                    });
                    that.map.basemap=that.vecBasemap;


                    // view.center = centerPt;
                    var html='比例尺：1:'+scale+'   '+'坐标:'+center[0]+'，'+center[0];
                    $(".showCorrd").html(html);
                });
        },

 		//切换
 		cutOverMap:function (newLayer) {
 			var that=this;
            var layerId = commonMapUtil.map.layers.items[0].id;
            var oldLayer =commonMapUtil.map.findLayerById(layerId);
            if (oldLayer)
            	commonMapUtil.map.remove(oldLayer);
            commonMapUtil.map.add(newLayer);
        },

        registerMapEvents:function () {
         	var that=this;
            var fullextent=null;
            var view=that.view;
            view.on('pointer-move',  function(evt){
                var point = view.toMap({x: evt.x, y: evt.y});
                var html='';
                var x=point.x.toFixed(4);
                var y=point.y.toFixed(4);
                html='比例尺：1:'+view.scale.toFixed(0)+'   '+'坐标:'+x+'，'+y;
                // $(".showCorrd").html(html);
            });
        }
    };


    CommonMapUtil.fn.init.prototype = CommonMapUtil.fn;
})();
